<!--
 * @Descripttion:
 * @version:
 * @Author: xj
 * @Date: 2024-11-11 09:56:40
 * @LastEditors: xj
 * @LastEditTime: 2024-11-11 16:34:03
-->
<template>
  <div class="p-2.5">
    <a-button type="primary" @click="checkUpdate">检查更新</a-button>

    <a-modal
      v-model:open="state.open"
      title="下载进度"
      centered
      okText="确定"
      cancelText="取消"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <div class="flex items-center justify-center mt-2">
        <a-progress type="circle" :percent="state.percent" />
      </div>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
  import { ipcRenderer } from 'electron'
  import { message, Modal } from 'ant-design-vue'

  console.log('项目根目录：', process.cwd())

  const state = reactive({
    open: false,
    percent: 0,
    showPro: false
  })

  onMounted(() => {
    ipcRenderer.on('update:msg', (_event, { code, data }) => {
      switch (code) {
        case -1:
          message.error(data)
          break
        case 0:
          break
        case 1:
          Modal.confirm({
            title: '提示',
            content: '检测到应用有新版本，是否更新？',
            type: 'warning',
            okText: '确定',
            cancelText: '取消',
            onOk() {
              ipcRenderer.invoke('app:downloadUpdate')
            }
          })
          break
        case 2:
          Modal.info({
            title: '提示',
            content: '当前使用的版本为最新版本,无需更新'
          })
          break
        case 3:
          state.open = true
          console.log('下载进度：', data)
          break
        case 4:
          Modal.confirm({
            title: '提示',
            content: '下载完成，是否立即退出安装应用？',
            okText: '确定',
            cancelText: '取消',
            onOk() {
              ipcRenderer.invoke('app:quitAndInstall')
            }
          })
      }
    })
  })

  function checkUpdate() {
    ipcRenderer.invoke('app:checkForUpdates')
  }

  function handleOk() {
    ipcRenderer.invoke('app:check-update')
    state.showPro = true
  }

  function handleCancel() {
    state.open = false
  }
</script>

<style scoped></style>
